<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form','url'));
		$this->lang->load('form_field','chinese');
	}

	public function index()
	{
		$this->load->library('form_validation');

		//$this->form_validation->set_rules('username', 'Username', 'required');
		//$this->form_validation->set_rules('password', 'Password', 'required');
		//$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');

		$config = array(
               array(
                     'field'   => 'username', 
                     'label'   => 'lang:form_field_username', 
                     'rules'   => 'trim|required|min_length[3]|callback_username_check'
                  ),
               array(
                     'field'   => 'password', 
                     'label'   => 'Password', 
                     'rules'   => 'required|matches[passconf]|md5'
                  ),
               array(
                     'field'   => 'passconf', 
                     'label'   => 'Password Confirmation', 
                     'rules'   => 'required|md5'
                  ),   
               array(
                     'field'   => 'email[]', 
                     'label'   => 'Email', 
                     'rules'   => 'required|valid_email'
                  )
            );

		$this->form_validation->set_rules($config);

		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('myform');
		}
		else
		{
			$this->load->view('formsuccess');
		}
	}

	 function username_check($str)
	 {
		  if ($str == 'test')
		  {
			  $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
			  return FALSE;
		  }
		  else
		  {
		      return TRUE;
		  }
	 }


}

/* End of file form.php */
/* Location: ./application/controllers/form.php */